package com.orange.modules.dao;

import java.sql.SQLException;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.orange.dbhelp.DbHelper;
import com.orange.dbhelp.page.Page;
import com.orange.modules.entity.manage.AuthOrgUser;

@Repository
public class AuthOrgUserRepository {
	
	@Resource(name = "dbHelper")
	private DbHelper dbHelper;
	
	@Autowired
	private AuthOrgUserDao authOrgUserDao;

	/**
	 * 根据账号查询管理员信息
	 * @param account
	 * @return
	 * @throws SQLException
	 */
	public AuthOrgUser getAuthOrgUserByAccount(String account,String type) throws SQLException{
		String sql = "select p_id as pid,s_user_account as userAccount,s_password as password ,s_create_time as createtime ,s_type as type FROM t_auth_org_user WHERE s_user_account = ? and s_type = ?";
		AuthOrgUser aou = dbHelper.getBean(sql, AuthOrgUser.class, account,type);
		return aou;
	}
	
	/**
	 * 保存管理员信息
	 * @param a
	 */
	public void saveAuthOrgUserInfo(AuthOrgUser a){
		authOrgUserDao.save(a);
	}
	
	/**
	 * 根据id查询管理员信息
	 * @param pid
	 * @return
	 */
	public AuthOrgUser queryAuthOrgUserById(String pid){
		AuthOrgUser a=authOrgUserDao.findOne(pid);
		return a;
		
	}
	/**
	 * 分页查询
	 * @throws SQLException 
	 * @throws NumberFormatException 
	 */
	public Page<AuthOrgUser> queryAuthOrgUserByPage(Map<String ,String> map) throws NumberFormatException, SQLException{
		StringBuilder sql = new StringBuilder("select TOP 1000 p_id as pid,s_user_account as userAccount,s_password as password ,s_create_time as createtime ,s_type as type FROM t_auth_org_user" +
				" where 1=1");
		if(StringUtils.isNotBlank(map.get("type"))){
			sql.append(" and s_type = '"+map.get("type")+"'");
		}
		if(StringUtils.isNotBlank(map.get("account"))){
			sql.append(" and s_user_account like '%"+map.get("account")+"%'");
		}
		sql.append(" ORDER BY s_create_time DESC ");
		Page<AuthOrgUser> page = dbHelper.getPage(sql.toString(), AuthOrgUser.class,Integer.valueOf(map.get("pageNumber")),
				Integer.valueOf(map.get("pageSize")), new Object[] {});
		return page;
	}
	
	/**
	 * 删除
	 */
	public void deleteAuthOrgUserInfo(String pid){
		authOrgUserDao.delete(pid);
	}
}
